package tech.aistar.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tech.aistar.entity.AppointmentTime;
import tech.aistar.entity.Concern;
import tech.aistar.entity.Register;
import tech.aistar.entity.Result;
import tech.aistar.service.IAppointmentTimeService;
import tech.aistar.service.IConcernService;
import tech.aistar.service.IRegisterService;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: shuaiguo
 * @Date: 2020/10/06/12:11
 * @Description:
 */

@Controller
@RequestMapping("/appointment")
public class AgentAppointmentController {
    @Autowired
    private IAppointmentTimeService appointmentTimeService;
@Autowired
private IConcernService concernService;
    @RequestMapping("/agent")
    public @ResponseBody
    Result select(HttpServletRequest request) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        String appointment_date =null;
        if(request.getParameter("appointment_date")!="") {
            appointment_date = request.getParameter("appointment_date");
        }

        String time_quantum = request.getParameter("time_quantum");
        String agent_id = request.getParameter("agent_id");
        String tenant_id = request.getParameter("tenant_id");
        String house_id = request.getParameter("house_id");
//        String appointment_state = request.getParameter("appointment_state");
        if(appointment_date ==null){
            return new Result("501", "日期不能为空");
        }


        List<AppointmentTime> appointmentTimes=appointmentTimeService.selectByTenantId(Integer.valueOf(tenant_id));
        for (AppointmentTime appointmentTime : appointmentTimes) {
                if (sdf.format(appointmentTime.getAppointmentDate()).equals(appointment_date) ) {
                    if(appointmentTime.getTimeQuantum().equals(time_quantum)) {

                        return new Result("503", "预约失败");
                    }
                }

        }

        try {
            List<AppointmentTime> appointmentTimes2=appointmentTimeService.selectAgentByAppointmentDate(sdf.parse(appointment_date),Integer.valueOf(agent_id));
            for (AppointmentTime appointmentTime : appointmentTimes2) {
                if (sdf.format(appointmentTime.getAppointmentDate()).equals(appointment_date) ) {
                    if(appointmentTime.getTimeQuantum().equals(time_quantum)) {

                        return new Result("504", "预约失败");
                    }
                }

            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        AppointmentTime appointmentTime =new AppointmentTime();
        appointmentTime.setAgentId(Integer.valueOf(agent_id));
        try {
            appointmentTime.setAppointmentDate(sdf.parse(appointment_date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        appointmentTime.setAppointmentState("预约中");
        appointmentTime.setHouseId(Integer.valueOf(house_id));
        appointmentTime.setTenantId(Integer.valueOf(tenant_id));
        appointmentTime.setTimeQuantum(time_quantum);
        appointmentTimeService.insert(appointmentTime);
        return new Result("200", "预约成功");

    }
    @RequestMapping("/concern")
    public @ResponseBody Result select2(HttpServletRequest request) {
        String tenant_id =null;
        String house_id =null;
        if(request.getParameter("tenant_id")!="")
            tenant_id = request.getParameter("tenant_id");
        if(request.getParameter("house_id") !="")
            house_id = request.getParameter("house_id");
        Concern concern = concernService.selectById(Integer.valueOf(tenant_id),Integer.valueOf(house_id));
        if(concern == null){
            return new Result("201","没有收藏");
        }
        return new Result("200", "有收藏");
    }



    @RequestMapping("/changeconcern")
    public @ResponseBody Result select3(HttpServletRequest request) {
        String tenant_id =null;
        String house_id =null;
        if(request.getParameter("tenant_id")!="")
         tenant_id = request.getParameter("tenant_id");
        if(request.getParameter("house_id") !="")
         house_id = request.getParameter("house_id");
        Concern concern = concernService.selectById(Integer.valueOf(tenant_id),Integer.valueOf(house_id));
        if(concern == null){
            concernService.insert(new Concern(Integer.valueOf(tenant_id),Integer.valueOf(house_id),new Date()));
            return new Result("202","加入收藏");
        }
        concernService.deleteByDoubleId(Integer.valueOf(tenant_id),Integer.valueOf(house_id));
        return new Result("203", "取消收藏");
    }
}
